package com.oauth.service.impl;

import com.oauth.mapper.OpenResourceAuthMapper;
import com.oauth.mapper.OpenTokenMapper;
import com.oauth.model.OpenResourceAuth;
import com.oauth.model.OpenToken;
import com.oauth.service.OpenResourceAuthService;
import com.oauth.service.OpenTokenService;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * Created by yangjianbo on 2016/9/29.
 */
@Service
public class OpenTokenServiceImpl implements OpenTokenService  {

    @Autowired
    private OpenTokenMapper openTokenMapper;

    @Autowired
    private OpenResourceAuthService openResourceAuthService;

    @Override
    public OpenToken insert(OpenToken openToken) {
        openToken.setTokenId(UUID.randomUUID().toString());
        openTokenMapper.insertSelective(openToken);
        return openToken;
    }

    @Override
    public OpenToken select(OpenToken openToken) {
        return openTokenMapper.selectOne(openToken);
    }

    @Override
    public OpenToken update(OpenToken openToken) {
         openTokenMapper.updateByPrimaryKeySelective(openToken);
         return openToken;
    }

    @Override
    public OpenToken select(String clientId, String code) {
        OpenToken openToken=new OpenToken();
        openToken.setClientId(clientId);
        openToken.setCode(code);
        return openTokenMapper.selectOne(openToken);
    }

    @Override
    public OpenToken select(String refresh_token) {
        OpenToken openToken=new OpenToken();
        openToken.setRefreshToken(refresh_token);
        return openTokenMapper.selectOne(openToken);    }

    @Override
    public OpenToken selectByAccessToken(String token) {
        OpenToken openToken=new OpenToken();
        openToken.setAccessToken(token);
        return openTokenMapper.selectOne(openToken);
    }

    @Override
    public Boolean isMatched(String client_id, String code, String redirec_uri) {
        OpenToken openToken=new OpenToken();
        openToken.setClientId(client_id);
        openToken.setCode(code);
        openToken.setRedirectUri(redirec_uri);
        OpenToken openToken1=openTokenMapper.selectOne(openToken);
        if(openToken1==null){
            return false;
        }else {
            return true;
        }
    }


}
